package action.admin.mclass;

import beans.ClassBean;
import dao.CheckAccount;
import dao.MngClass;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class createClass extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String class_name = request.getParameter("class_name");
            String acc_id = request.getParameter("acc_id");
            String start_date = request.getParameter("start_date");
            String finish_date = request.getParameter("finish_date");
            String description = request.getParameter("description");
            ClassBean classBean = new ClassBean();
            classBean.setClass_name(class_name);
            classBean.setAcc_id(acc_id);
            classBean.setStart_date(start_date);
            classBean.setFinish_date(finish_date);
            classBean.setDescription(description);
            MngClass mngClass = new MngClass();
            String msg = "";
            String error = "";
            if (!class_name.equals("") && class_name != null) {
                if (!acc_id.equals("") && acc_id != null) {
                    if (!mngClass.checkClassName(class_name)) {
                        if (mngClass.createClass(classBean)) {
                            msg = "Create successful !";
                            request.setAttribute("msg", msg);
                            RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=class");
                            rd.forward(request, response);
                        } else {
                            error = "Create class error!";
                            request.setAttribute("error", error);
                            RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=create_class");
                            rd.forward(request, response);
                        }
                    } else {
                        error = "Class Name has exist !";
                        request.setAttribute("error", error);
                        RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=create_class");
                        rd.forward(request, response);
                    }
                } else {
                    error = "Insert Staff Id!";
                    request.setAttribute("error", error);
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=create_class");
                    rd.forward(request, response);
                }
            } else {
                error = "Insert class name!";
                request.setAttribute("error", error);
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=create_class");
                rd.forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
